package org.hrc.puzzleGame.mapper.impl;

import org.hrc.puzzleGame.model.User;
import org.hrc.puzzleGame.mapper.UserMapper;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserMapperImpl implements UserMapper {

    @Override
    public void addUser(User user) throws SQLException {

        String sql = "insert into puzzlegame.user (userName, password, state, createTime, updateTime) values (?, ?, ?, current_time, current_time)";
          //预编译sql语句
          PreparedStatement psmt = connection.prepareStatement(sql);
          //先对应SQL语句，给SQL语句传递参数
          psmt.setString(1, user.getUserName());
          psmt.setString(2, user.getPassword());
          psmt.setInt(3, user.getState());
          //执行SQL语句
          psmt.execute();
        System.out.println(user.getUserName() + "添加成功");
    }

    @Override
    public void updateUser(User user) throws SQLException {
        String sql = "update puzzlegame.user set userName = ?, password = ?, state = ?, updateTime = current_date, ranking = ?, step = ? where id = ?";
        PreparedStatement psmt = connection.prepareStatement(sql);
        psmt.setString(1, user.getUserName());
        psmt.setString(2, user.getPassword());
        psmt.setInt(3, user.getState());
        if (user.getRaking() != null)
            psmt.setInt(4, user.getRaking());
        else
            psmt.setInt(4, 999);
        psmt.setInt(5, user.getStep());
        psmt.setInt(6, user.getId());
        psmt.execute();
        System.out.println("更新成功");
    }

    @Override
    public void deleteUser(Integer id) throws SQLException {
        String sql = "delete from puzzlegame.user  where id = ?";
        PreparedStatement psmt = connection.prepareStatement(sql);
        psmt.setInt(1, id);
        psmt.execute();
        System.out.println("删除成功");
    }

    @Override
    public User queryById(Integer id) throws SQLException {
        String sql = "select * from puzzlegame.user where  id = ?";
        PreparedStatement psmt = connection.prepareStatement(sql);
        psmt.setInt(1, id);
        ResultSet resultSet = psmt.executeQuery();
        resultSet.next();
        User user = setUser(resultSet);
        return user;
    }

    @Override
    public List<User> queryAll() throws SQLException {
        List<User> list = new ArrayList<>();
        String sql = "select * from puzzlegame.user ";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            list.add(setUser(resultSet));
        }
        return list;
    }

    @Override
    public User queryByName(String name) throws SQLException {
         String sql = "select  * from puzzlegame.user where userName = ?";
        PreparedStatement psmt = connection.prepareStatement(sql);
        psmt.setString(1, name);
        ResultSet resultSet = psmt.executeQuery();
        if (!resultSet.next())
            return null;
        User user = setUser(resultSet);
        return user;
    }

    public User setUser(ResultSet resultSet) throws SQLException {
        User user = new User();
        user.setId(resultSet.getInt("id"));
        user.setUserName(resultSet.getString("userName"));
        user.setPassword(resultSet.getString("password"));
        user.setRaking(resultSet.getInt("ranking"));
        user.setState(resultSet.getInt("state"));
        user.setCreateTime(resultSet.getDate("createTime"));
        user.setUpdateTime(resultSet.getDate("updateTime"));
        user.setStep(resultSet.getInt("step"));
        return user;
    }
}
